package org.example.game.history;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/07/07 10:49
 */
public class Solution100359 {

    public static void main(String[] args) {
        System.out.println(new Solution100359().numberOfSubmatrices(new char[][]{{'.','.','.'},{'.','X','X'},{'Y','.','.'},{'X','.','.'}}));
    }


    public int numberOfSubmatrices(char[][] grid) {
        int[][][] gridCount = new int[grid.length][grid[0].length][2];
        int sum = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {

                int xNum = 0;
                int yNum = 0;
                if('X' == grid[i][j]){
                    xNum++;
                }else if ('Y' == grid[i][j]){
                    yNum++;
                }
                if(i == 0 && j == 0){
                    gridCount[i][j][0] = xNum;
                    gridCount[i][j][1] = yNum;
                }else if(i == 0){
                    gridCount[i][j][0] = xNum + gridCount[i][j-1][0];
                    gridCount[i][j][1] = yNum + gridCount[i][j-1][1];
                }else if (j == 0){
                    gridCount[i][j][0] = xNum + gridCount[i-1][j][0];
                    gridCount[i][j][1] = yNum + gridCount[i-1][j][1];
                }else {
                    gridCount[i][j][0] = xNum + gridCount[i-1][j][0] + gridCount[i][j-1][0] - gridCount[i-1][j-1][0];
                    gridCount[i][j][1] = yNum + gridCount[i-1][j][1] + gridCount[i][j-1][1] - gridCount[i-1][j-1][1];
                }

                if(gridCount[i][j][0] > 0 && gridCount[i][j][0] == gridCount[i][j][1]){
                    sum++;
                }
            }
        }
        return sum;
    }

    public int numberOfSubmatrices2(char[][] grid) {

        //横 竖 斜 (累计数量)  还有 优先横 优先竖
        int sum = 0;


       int xNum = 0;
       int yNum = 0;

        for (int i = 0; i < grid.length; i++) {

            if('X' == grid[i][0]){
                xNum++;
            }else if ('Y' == grid[i][0]){
                yNum++;
            }
            if(xNum == yNum && xNum > 0){
                sum++;
            }
        }

        xNum = 0;
        yNum = 0;
        for (int i = 0; i < grid[0].length; i++) {
            if('X' == grid[0][i]){
                xNum++;
            }else if ('Y' == grid[0][i]){
                yNum++;
            }
            if(xNum == yNum && xNum > 0){
                sum++;
            }
        }

        xNum = 0;
        yNum = 0;

        int xLen = grid[0].length;
        int yLen = grid.length;

        xLen = Math.min(xLen, yLen);

        for (int i = 0; i < xLen; i++) {

            for (int j = 0; j <= i; j++) {
                if('X' == grid[i][j]){
                    xNum++;
                }else if ('Y' == grid[i][j]){
                    yNum++;
                }
            }

            for (int j = 0; j < i; j++) {
                if('X' == grid[j][i]){
                    xNum++;
                }else if ('Y' == grid[j][i]){
                    yNum++;
                }
            }

            if(xNum == yNum && xNum > 0){
                sum++;
            }

        }

        return sum;
    }

}
